<?php

Yii::import('user.controllers.UserBaseController');

class LoginController extends UserBaseController
{
	public $layout='/layouts/column1';

	public function actionIndex()
	{
		$this->setPageTitle('Đăng nhập hệ thống quản trị');
		if(Yii::app()->user->id) $this->redirect($this->createUrl('/hq/default/index'));
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];

//			 validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
			{
				if(isset(Yii::app()->user->returnUrl)){
					$this->redirect($this->createUrl('/hq/default/index'));
				}
				else  $this->redirect($this->createUrl('/hq/'));
			}
		}

		// display the login form

		$this->render('index',array('model'=>$model));
	}
}

?>
